/*
	FileName : app_led.c
	Description : 
	
	Created on 2013.10.27 by ZZP.
	
	Revision History:
	
*/

 
#include "bsp.h"
#include "app.h"
#include "app_led.h"

//-------------------------------------------------------------
APP_LED_STR JLB_M_led;

void USB_Led(void);
void STM_Led(void);
void CFC_Led(void);
void DTU_Led(void);
void ERR_Led(void);

void MiscLed(void);
/////////////////////////////////////////////////////////////////////////////////
///							Global Function realizations
/////////////////////////////////////////////////////////////////////////////////
void AppTaskLED(void *p_arg)
{
	(void)p_arg;

	while (DEF_TRUE){
		
		if (AppInfo.RdyFlag){
			
			USB_Led();	//USB
			STM_Led();	//STM
			CFC_Led();	//CFC
			DTU_Led();	//DTU
			ERR_Led();	//ERR
			
			MiscLed();	//UART_TX_LED
		}
		
		AppInfo.TaskLedCnt++;
		
		OSTimeDly(10);
	}
}
//------------------------------------------------------------------------------
uint8_t Led_flash(uint8_t led,uint16_t t1,uint16_t t2,uint16_t t3,uint16_t t4)
{
	uint32_t systime;
	
	systime = HAL_GetTick();
	systime %= (t1 + t2 + t3 + t4);
	
	if (systime < t1 || (systime > t1 + t2 && systime < t1 + t2 + t3))
	{
		BSP_LED_On(led);
		return 1;
	}
	else
	{
		BSP_LED_Off(led);
		return 0;
	}
}
//--------------------------------------------

void ERR_Led(void)
{
	if(JLB_M_led.ERRRTCFlag == ERR_LED_STATUS_RTC_ERR)
	{
		Led_flash(LED_ERR,25,25,0,0);
		return;
	}
	if(JLB_M_led.ERRLedFlag == ERR_LED_STATUS_CF_ERR)
	{
		Led_flash(LED_ERR,125,375,0,0);
		return;
	}
	if(JLB_M_led.ERRCOMFlag == ERR_LED_STATUS_SAMPLE_DISCONN)
	{
		Led_flash(LED_ERR,1000,1000,0,0);
	}
	else
	{
		BSP_LED_Off(LED_ERR);
	}
}
void DTU_Led(void)
{
	static uint8_t on_off = 0;
	uint16_t t1,t2;
	
	switch (JLB_M_led.DTULedFlag){
		
		case DTU_LED_STATUS_COMMUNICATING:
			t1 = 75;
			t2 = 2575;
		default:
			t1 = 0;
			t2 = 4000;
			break;	
	}
	if(on_off != Led_flash(LED_DTU,t1,t2,0,0))
	{
		on_off = ((~on_off) & 0x01);
		if(JLB_M_led.DTUFlashCnt)
		{
			JLB_M_led.DTUFlashCnt--;
			if(JLB_M_led.DTUFlashCnt == 0)
				JLB_M_led.DTULedFlag = DTU_LED_STATUS_UNCONNECTED;
		}
	}
}

void CFC_Led(void)
{
	static uint8_t on_off = 0;
	uint16_t t1,t2;
	
	if(JLB_M_led.CFCSDFlag == CFC_LED_STATUS_SDFORMAT)
	{
		Led_flash(LED_CFC,250,250,0,0);
		return;
	}
	
	if (AppInfo.SD_Error)
	{
		JLB_M_led.CFCLedFlag = CFC_LED_STATUS_ERROR;
		JLB_M_led.CFCFlashCnt = 0;
	}
	
	switch (JLB_M_led.CFCLedFlag){
		
		case CFC_LED_STATUS_IDLE:
			t1 = 0;
			t2 = 1975;
			break;
		case CFC_LED_STATUS_OPERATE:
			t1 = 25;
			t2 = 25;
			break;
		case CFC_LED_STATUS_WAVE:
			t1 = 1350;
			t2 = 150;
			break;
		default:
			t1 = 3975;
			t2 = 25;
			break;	
	}
	if(on_off != Led_flash(LED_CFC,t1,t2,0,0))
	{
		on_off = ((~on_off) & 0x01);
		if(JLB_M_led.CFCFlashCnt)
		{
			JLB_M_led.CFCFlashCnt--;
			if(JLB_M_led.CFCFlashCnt == 0)
				JLB_M_led.CFCLedFlag = CFC_LED_STATUS_IDLE;
		}
	}
}
void STM_Led(void)
{
	uint16_t t1,t2;
	
	switch (JLB_M_led.STMLedFlag){
		
		case STM_LED_STATUS_ERROR:
			t1 = 125 / 2;
			t2 = 875 / 2;
			break;
		default:
			t1 = 250;
			t2 = 1750;
			break;
	}
	Led_flash(LED_STM,t1,t2,0,0);
}
void USB_Led(void)
{
	if(JLB_M_led.USBLedFlag == USB_LED_STATUS_USB_ERROR){
		
		Led_flash(LED_USB,3975,25,0,0);
		return;
	}
	
	if (AppInfo.USB_Status)
		BSP_LED_On(LED_USB);
	else
		BSP_LED_Off(LED_USB);
}
//-----------------------------------------------------------------------------

//----------------------------------------------------------------------------------
void MiscLed(void)
{
	if (AppInfo.MiscLedFlag){
		
		if (AppInfo.MiscLedTm >= 3){
			
			AppInfo.MiscLedTm = 0;
			AppInfo.MiscLedFlag--;
		}
		else if (AppInfo.MiscLedTm > 1)
			BSP_LED_Off(LED_MISC);
		else
			BSP_LED_On(LED_MISC);
		
		AppInfo.MiscLedTm++;
	}
	else
		AppInfo.MiscLedTm = 0;
}

////////////////////////////////////////////////////////////////////////////////

void LedFlagUpdate(uint8_t rtcErr,uint8_t ctrlErr,uint32_t powerOnTime)
{
	///STM LED
	if(ctrlErr)
		JLB_M_led.STMLedFlag = STM_LED_STATUS_ERROR;
	else
		JLB_M_led.STMLedFlag = STM_LED_STATUS_NORMAL;
	
	if (rtcErr && powerOnTime >= 1000)
	{
		JLB_M_led.ERRRTCFlag = ERR_LED_STATUS_RTC_ERR;
	}
	else
	{
		JLB_M_led.ERRRTCFlag = ERR_LED_STATUS_RTC_OK;
	}
	
	return;
}
//--------------------------------------------------------------------------------
///EOF








